//
//  MBWhoLikeMeDataManager.m
//  Meboo
//
//  Created by kyo on 2023/10/21.
//

#import "MBWhoLikeMeDataManager.h"
#import "MBHTTPNetworking.h"
@implementation MBWhoLikeMeDataManager
+ (MBWhoLikeMeDataManager *)sharedManager {
    static dispatch_once_t onceToken;
    static MBWhoLikeMeDataManager *obj;
    dispatch_once(&onceToken, ^{
        obj = [[self alloc] init];
    });
    return obj;
}

#pragma mark -获取who like me 列表
- (void)getWhoLikeMeList:(void(^)(BOOL success))resultBlock {
    /**
     埋点 code
     WLM流量下发 206
     */
    [[NSIOApiManager sharedManager] setIOWithCode:@"206" content:@"SEND_WLM"];
    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/wlmList" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSLog(@"");
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            NSArray *dataArr = responseObject[@"data"][@"wlmList"];
            self.wlmNum = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"wlmUsageNum"]];
            [weakSelf.listDataArr removeAllObjects];
            for (NSDictionary *dict0 in dataArr) {
                MBWLMTopicModel *model0 = [MBWLMTopicModel mj_objectWithKeyValues:dict0];
                [model0.interests removeAllObjects];
                for (NSDictionary *dict1 in dict0[@"interests"]) {
                    MBWLMTopicImageModel *model1 = [MBWLMTopicImageModel mj_objectWithKeyValues:dict1];
                    [model0.interests addObject:model1];
                }
                //test
//                if (weakSelf.listDataArr.count<2) {
                    [weakSelf.listDataArr addObject:model0];
//                }
                    
            }
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -获取音乐卡列表 homepage
- (void)getHomePageList:(void(^)(BOOL success))resultBlock {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/home/homepage" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSLog(@"");
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            //likeNum重置
            MBUserAccount *account = [MBUserAccount defaultAccount];
            account.likeNum = 0;
            [MBUserAccount saveAccount:account];
            
            self.homeModelNum = [responseObject[@"data"][@"remainingBrowseBNum"] integerValue];
            self.homeLikeNum = [responseObject[@"data"][@"remainingWlmNum"] integerValue];
            self.homeNullState = [responseObject[@"data"][@"preferentialFlag"] integerValue];
            self.refreshTime = [responseObject[@"data"][@"refreshTime"] integerValue];
            self.title = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"title"]];;
            NSArray *dataArr = responseObject[@"data"][@"homepageList"];
            [self.topicDataArr removeAllObjects];
            if ([self isEmpty:dataArr]) {
                
            } else {
                for (NSDictionary *dict0 in dataArr) {
                    
                    MBWLMTopicModel *model0 = [MBWLMTopicModel mj_objectWithKeyValues:dict0];
                    [model0.images removeAllObjects];
                    [model0.interests removeAllObjects];
                    for (NSDictionary *dict1 in dict0[@"images"]) {
                        MBWLMTopicImageModel *model1 = [MBWLMTopicImageModel mj_objectWithKeyValues:dict1];
                        if (![model1.imageUrl containsString:@"video"]) {
                            [model0.images addObject:model1];
                        }
                    }
                    for (NSDictionary *dict2 in dict0[@"interests"]) {
                        MBWLMTopicImageModel *model2 = [MBWLMTopicImageModel mj_objectWithKeyValues:dict2];
                        [model0.interests addObject:model2];
                    }
                    
//                    if (self.topicDataArr.count < 4) {
//                        [self.topicDataArr addObject:model0];
//                    }
                    if ([MBUserAccount defaultAccount].isVip) {
                        [self.topicDataArr addObject:model0];
                    } else if(self.topicDataArr.count < self.homeModelNum) {
                        [self.topicDataArr addObject:model0];
                    }
                }
            }
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -用户喜欢或者不喜欢  1:like  4:pass
- (void)topicLikeType:(NSInteger)type userId:(NSString *)userId success:(void(^)(BOOL success))resultBlock {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userId.length > 0 ? userId : @"" forKey:@"userCodeFriend"];
    [params setObject:@(type) forKey:@"likeType"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/addWlm" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            BOOL matched = [responseObject[@"data"][@"matched"] boolValue];
            if (resultBlock) resultBlock(matched);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -自定义通知 6/7 sayhi   1:like 2:dislike
- (void)noticSayHiWithType:(NSInteger)type userId:(NSString *)userId success:(void(^)(BOOL success))resultBlock {
    if (type == 1) {
        /**
         埋点 code
         LIKE BACK流量下发 207
         */
        [[NSIOApiManager sharedManager] setIOWithCode:@"207" content:@"LIKE_BACK"];
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userId.length > 0 ? userId : @"" forKey:@"userCodeFriend"];
    [params setObject:@(type) forKey:@"likeType"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/benefits/reduceWLM" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            if (resultBlock) resultBlock(YES);
        } else if (code == 2003) {
            if (resultBlock) resultBlock(NO);
            //提示购买会员
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -自定义通知 8 查询次数
- (void)notiMatchWithUserId:(NSString *)userId success:(void(^)(BOOL success))resultBlock {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userId.length > 0 ? userId : @"" forKey:@"toUserId"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/im/message/tryToSend" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}


- (NSMutableArray<MBWLMTopicModel *> *)topicDataArr {
    if (!_topicDataArr) {
        _topicDataArr = [NSMutableArray array];
    }
    return _topicDataArr;
}

- (NSMutableArray<MBWLMTopicModel *> *)listDataArr {
    if (!_listDataArr) {
        _listDataArr = [NSMutableArray array];
    }
    return _listDataArr;
}

- (BOOL)isEmpty:(id)object {
    
    if(object == nil || [object isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    
    if([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *empty = (NSDictionary*)object;
        if (empty.allKeys == 0) {
            return YES;
        }
    }
    else if([object isKindOfClass:[NSArray class]])
    {
        NSArray *empty = (NSArray*)object;
        if (empty.count == 0){
            return YES;
        }
    }
    else if ([object isKindOfClass:[NSString class]])
    {
        if([(NSString *)object isEqualToString:@""] || [(NSString *)object isEqualToString:@"<null>"])
            return YES;
    }
    else if ([object isKindOfClass:[NSNumber class]])
    {
        if (object == 0) {
            return YES;
        }
        
    }
    else if ([object isKindOfClass:[NSSet class]])
    {
        NSSet *empty = (NSSet*)object;
        if (empty.count == 0){
            return YES;
        }
    }
    
    return NO;
}
@end
